<?php


namespace App\Modules\Admin\Services\Seller;


use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Seller\SellerRoleModel;
use App\Modules\Admin\Services\BaseService;
use App\Modules\Seller\Entities\Rabc\SellerAdmin as SellerAdminModel;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class SellerAdminServices extends BaseService
{
    protected $guard = 'seller_admin';
    public function __construct(SellerAdminModel $sellerAdminModel)
    {
        $this->model = $sellerAdminModel;
        $this->with = [];
    }

    /**
     * 菜单列表
     *
     * @param array $params
     * @return array
     */
    public function lists(array $params) : array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            $seller_id= $request->input('seller_id', 0);
            $seller_role_id= $request->input('seller_role_id', 0);
            $query->where(['seller_id'=>$seller_id]);
            $query->where(['role_id'=>$seller_role_id]);
            // 按照名称进行搜索
            if (!empty($search = $request->input('search', ''))){
                $query->where('account', 'LIKE', '%' . trim($search) . '%');
            }
            // 状态
            $is_check = $request->input('is_check', -1);
            if ($is_check > -1){
                $query->where('is_check', '=', $is_check);
            }
        };
        return  parent::lists($params);
    }

    public function create(array $params)
    {
        DB::beginTransaction();
        try{
            $params['created_time'] = time();
            parent::create($params); // TODO: Change the autogenerated stub
            // 管理员关联角色设置
            $this->setRoleForAdmin($params);
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            $this->setError($e->getMessage());
            return false;
        }
    }
    public function update(array $params)
    {
        DB::beginTransaction();
        try{
            $params['updated_time'] = time();
            parent::update($params); // TODO: Change the autogenerated stub
            // 管理员关联角色设置
            $this->setRoleForAdmin($params);
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            $this->setError($e->getMessage());
            return false;
        }
    }
    /**
     * 管理员关联角色设置
     *
     * @param  array  $request_data
     */
    private function setRoleForAdmin(array $request_data)
    {
        if (!isset($request_data['role_ids'])) return;
        /**
         * 角色操作
         */
        $request_roles = $request_data['role_ids'] ?? [];
        $all_roles     = SellerRoleModel::findMany($request_roles);//当前选中的角色
        $has_roles     = $this->detail->roles;//当前已有的角色


        /**
         * 添加的角色
         */
        $add_roles = $all_roles->diff($has_roles);
        if (!empty($add_roles)) {
            foreach ($add_roles as $roles)
                $this->detail->assignRole($roles);
        }
        /**
         * 要删除的角色
         */
        $delete_roles = $has_roles->diff($all_roles);
        if (!empty($delete_roles)) {
            foreach ($delete_roles as $roles) $this->detail->deleteRole($roles);
        }
    }
    public function getSelectLists($request)
    {
        return $this->model->getSelectLists();
    }
}